﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： UserCardRepository.cs
// 项目名称：斑马车务
// 创建时间：2019/10/14
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using Dapper.Extensions;
using YouWei.Infrastructure.Models;
using Dapper;

namespace YouWei.Infrastructure.Repository
{
	/// <summary>
	/// 仓储层
	/// </summary>	
	public class UserCardRepository:BaseRepository<UserCard>
	{		
    
        #region 构造

        public UserCardRepository() : base(){}

        public UserCardRepository(string connectionString) : base(connectionString){}

        public UserCardRepository(DatabaseContext dbContext) : base(dbContext){}

        #endregion

        /// <summary>
        /// 分页查询，带条件
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public Page<UserCardDto> GetPages(int pageIndex, long pageSize, ReqUserCardCondition condition)
        {
            StringBuilder sql = new StringBuilder("SELECT * FROM UserCard WITH(NOLOCK) where IsActive =1");
            DynamicParameters dparams = new DynamicParameters();

            if (!string.IsNullOrWhiteSpace(condition.StartDate))
            {
                sql.Append(" AND CreatedDate>@StartDate");
                dparams.Add("@StartDate", condition.StartDate, System.Data.DbType.String);
            }
            if (!string.IsNullOrWhiteSpace(condition.EndDate))
            {
                sql.Append(" AND CreatedDate<@EndDate");
                dparams.Add("@EndDate", condition.EndDate, System.Data.DbType.String);
            }
            if (!string.IsNullOrWhiteSpace(condition.Name))
            {
                sql.Append(" AND CHARINDEX(@Name,Name) >0");
                dparams.Add("@Name", condition.Name, System.Data.DbType.String);
            }
            if (condition.Level>0)
            {
                sql.Append(" AND Level=@Level");
                dparams.Add("@Level", condition.Level, System.Data.DbType.Int32);
            }
                     
            return dbContext.Connection.Page<UserCardDto>(pageIndex, pageSize, sql.ToString(), dparams);
        }

        /// <summary>
        /// 获取客户卡列表
        /// </summary>
        /// <returns></returns>
        public List<UserCard> GetList()
        {
            string sql = "SELECT * FROM UserCard WITH(NOLOCK) where IsActive =1";
            return Query(sql);
        }

        /// <summary>
        /// 获取客户卡列表
        /// </summary>
        /// <returns></returns>
        public List<UserCardInfo> GetListByUserId(int userId)
        {
            DynamicParameters dparams = new DynamicParameters();
            string sql = "SELECT * FROM UserCard cd WITH(NOLOCK) where cd.IsActive = 1";

            if (userId>0)
            {
                sql = @"SELECT  cd.*,
                                re.CardCode,
								re.RelaId,
	                         	re.UpdatedDate
                        FROM    UserCard cd WITH ( NOLOCK )
                                LEFT JOIN UserCardRela re WITH ( NOLOCK ) ON cd.CardId = re.CardId
                        WHERE   cd.IsActive = 1 AND re.IsActive = 1 AND re.UserId=@UserId";
                dparams.Add("@UserId", userId, System.Data.DbType.Int32);
            }
    
            return dbContext.Connection.Query<UserCardInfo>(sql.ToString(), dparams).AsList();
        }

        /// <summary>
        /// 获取客户卡内容列表
        /// </summary>
        /// <returns></returns>
        public UserCardModel GetCardById(int carId)
        {
            string sql = @" SELECT  t.*  FROM UserCard t WITH ( NOLOCK ) WHERE  t.[CardId] = @CardId";
            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@CardId", carId, System.Data.DbType.Int32);
            return dbContext.Connection.QueryFirstOrDefault<UserCardModel>(sql.ToString(), dparams);
        }


        /// <summary>
        /// 获取客户卡内容列表
        /// </summary>
        /// <returns></returns>
        public List<UserCardModel> GetCardByLevel(int level)
        {
            string sql = @" SELECT  t.*  FROM UserCard t WITH ( NOLOCK ) WHERE  t.[Level] = @Level";
            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@Level", level, System.Data.DbType.Int32);
            return dbContext.Connection.Query<UserCardModel>(sql.ToString(), dparams).AsList();
        }
    }
}